package com.buyingtickets.controller;

import com.buyingtickets.entity.Order;
import com.buyingtickets.entity.QueryParameter;
import com.buyingtickets.entity.Ticket;
import com.buyingtickets.entity.Type;
import com.buyingtickets.service.OrderBiz;
import com.buyingtickets.service.TypeBiz;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author czl
 * @version 1.0
 * @description: TODO
 * @date 2021/6/25 10:03
 */
@Controller
@RequestMapping("order")
public class OrderController{
    @Resource
    private OrderBiz orderBiz;
    @Resource
    private TypeBiz typeBiz;
    @RequestMapping("query")
    public String order(QueryParameter qp,Model model){
        List<Order> orderList=orderBiz.queryByQueryOrder(qp);
         model.addAttribute("orderList",orderList);
         model.addAttribute("qp",qp);
        return "admin_order";
    }

    @RequestMapping("{page}/{yonghu}/{dingdanhao}/{checi}/{facheriqi}/fenye")
    public String fenye(@PathVariable int page,@PathVariable String yonghu, @PathVariable String dingdanhao, @PathVariable String checi, @PathVariable String facheriqi, Model model){
        System.out.println(page+" "+yonghu+" "+dingdanhao+" "+checi+" "+facheriqi);
        QueryParameter qp=new QueryParameter();
        if (!"!".equals(page)) {
            qp.setPage(page);
        }
        if (!"!".equals(yonghu)) {
            qp.setYonghu(yonghu);
        }
        if (!"!".equals(dingdanhao)) {
            qp.setDingdanhao(dingdanhao);
        }
        if (!"!".equals(checi)) {
            qp.setCheci(checi);
        }
        if (!"!".equals(facheriqi)) {
            qp.setFacheriqi(facheriqi);
        }

        return order(qp,model);
    }
    @RequestMapping("order_details/{id}")
    public String order_details(@PathVariable int id,Model model){
        List<Type> dingdanList=typeBiz.queryByDingDanZhuangTai();
        Order order=orderBiz.queryByID(id);
        model.addAttribute("dingdanList",dingdanList);
        model.addAttribute("order",order);
        return "admin_order_details";
    }


    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public int update(Order order){
        System.out.println(order);
        return orderBiz.update(order);
    }

    @RequestMapping( "delete")
    @ResponseBody
    public int delete(int id){
      return orderBiz.delete(id);
    }

    @RequestMapping("queryticket")
    public String ticket(QueryParameter qp,Model model){
        List<Ticket> ticketList=orderBiz.queryByQueryTicket(qp);
        model.addAttribute("ticketList",ticketList);
        model.addAttribute("qp",qp);
        return "admin_ticket";
    }

    @RequestMapping("{page}/{chengke}/{dingdanhao}/{checi}/{facheriqi}/fenye2")
    public String fenye2(@PathVariable int page,@PathVariable String chengke, @PathVariable String dingdanhao, @PathVariable String checi, @PathVariable String facheriqi, Model model){
        System.out.println(page+" "+chengke+" "+dingdanhao+" "+checi+" "+facheriqi);
        QueryParameter qp=new QueryParameter();
        if (!"!".equals(page)) {
            qp.setPage(page);
        }
        if (!"!".equals(chengke)) {
            qp.setYonghu(chengke);
        }
        if (!"!".equals(dingdanhao)) {
            qp.setDingdanhao(dingdanhao);
        }
        if (!"!".equals(checi)) {
            qp.setCheci(checi);
        }
        if (!"!".equals(facheriqi)) {
            qp.setFacheriqi(facheriqi);
        }

        return ticket(qp,model);
    }

    @RequestMapping("ticket_details/{id}")
    public String ticket_details(@PathVariable int id,Model model){
        List<Type> chepiaoList=typeBiz.queryByChePiaoZhuangTai();
        Ticket ticket=orderBiz.queryByTID(id);
        model.addAttribute("chepiaoList",chepiaoList);
        System.out.println(ticket);
        model.addAttribute("ticket",ticket);
        return "admin_ticket_details";
    }
    @RequestMapping(value = "updateT",method = RequestMethod.POST)
    @ResponseBody
    public int updateT(Ticket ticket){
        System.out.println(ticket);
        return orderBiz.updateT(ticket);
    }
    @RequestMapping( "deleteT")
    @ResponseBody
    public int deleteT(int id){
        return orderBiz.deleteT(id);
    }
}
